/*--------------------------------*- C++ -*----------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     | Website:  https://openfoam.org
    \\  /    A nd           | Version:  dev
     \\/     M anipulation  |
\*---------------------------------------------------------------------------*/
FoamFile
{
    format      ascii;
    class       dictionary;
    object      blockMeshDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

convertToMeters 0.01;

rCore 6;
rPipe 10;
rBall 18;
lPipe 50;

nCore 10;
nPipe 7;
nLength 20;

rBend #calc "sqrt(scalar($rBall*$rBall - $rPipe*$rPipe))";

geometry
{
    sphere
    {
        type sphere;
        centre (0 0 0);
        radius $rBall;
    }
    torus
    {
        type    triSurface;
        file    "ballValve-torus.obj";
    }
}

vertices
(
    // X-pipe
    name vx0 (#neg $lPipe #neg $rCore 0)
    name vx1 (#neg $lPipe 0 #neg $rCore)
    name vx2 (#neg $lPipe $rCore 0)
    name vx3 (#neg $lPipe 0 $rCore)

    name vx4 (#neg $lPipe #neg $rPipe 0)
    name vx5 (#neg $lPipe 0 #neg $rPipe)
    name vx6 (#neg $lPipe $rPipe 0)
    name vx7 (#neg $lPipe 0 $rPipe)

    name vx8 project (#neg $rBend #neg $rCore 0) (sphere)
    name vx9 project (#neg $rBend 0 #neg $rCore) (sphere)
    name vx10 project (#neg $rBend $rCore 0) (sphere)
    name vx11 project (#neg $rBend 0 $rCore) (sphere)

    name vx12 (#neg $rBend #neg $rPipe 0)
    name vx13 (#neg $rBend 0 #neg $rPipe)
    name vx14 (#neg $rBend $rPipe 0)
    name vx15 (#neg $rBend 0 $rPipe)

    // Y-lower-pipe
    name vl0 ($rCore #neg $lPipe 0)
    name vl1 (0 #neg $lPipe #neg $rCore)
    name vl2 (#neg $rCore #neg $lPipe 0)
    name vl3 (0 #neg $lPipe $rCore)

    name vl4 ($rPipe #neg $lPipe 0)
    name vl5 (0 #neg $lPipe #neg $rPipe)
    name vl6 (#neg $rPipe #neg $lPipe 0)
    name vl7 (0 #neg $lPipe $rPipe)

    name vl8 project ($rCore #neg $rBend 0) (sphere)
    name vl9 project (0 #neg $rBend #neg $rCore) (sphere)
    name vl10 project (#neg $rCore #neg $rBend 0) (sphere)
    name vl11 project (0 #neg $rBend $rCore) (sphere)

    name vl12 ($rPipe #neg $rBend 0)
    name vl13 (0 #neg $rBend #neg $rPipe)
    name vl14 (#neg $rPipe #neg $rBend 0)
    name vl15 (0 #neg $rBend $rPipe)

    // Y-upper-pipe
    name vu0 (#neg $rCore $lPipe 0)
    name vu1 (0 $lPipe #neg $rCore)
    name vu2 ($rCore $lPipe 0)
    name vu3 (0 $lPipe $rCore)

    name vu4 (#neg $rPipe $lPipe 0)
    name vu5 (0 $lPipe #neg $rPipe)
    name vu6 ($rPipe $lPipe 0)
    name vu7 (0 $lPipe $rPipe)

    name vu8 project (#neg $rCore $rBend 0) (sphere)
    name vu9 project (0 $rBend #neg $rCore) (sphere)
    name vu10 project ($rCore $rBend 0) (sphere)
    name vu11 project (0 $rBend $rCore) (sphere)

    name vu12 (#neg $rPipe $rBend 0)
    name vu13 (0 $rBend #neg $rPipe)
    name vu14 ($rPipe $rBend 0)
    name vu15 (0 $rBend $rPipe)

    // Ball
    name vb0 project (#neg $rBend #neg $rCore 0) (sphere)
    name vb1 project (#neg $rBend 0 #neg $rCore) (sphere)
    name vb2 project (#neg $rBend $rCore 0) (sphere)
    name vb3 project (#neg $rBend 0 $rCore) (sphere)

    name vb4 (#neg $rBend #neg $rPipe 0)
    name vb5 (#neg $rBend 0 #neg $rPipe)
    name vb6 (#neg $rBend $rPipe 0)
    name vb7 (#neg $rBend 0 $rPipe)

    name vb8 project (#neg $rCore #neg $rBend 0) (sphere)
    name vb9 project (0 #neg $rBend #neg $rCore) (sphere)
    name vb10 project ($rCore #neg $rBend 0) (sphere)
    name vb11 project (0 #neg $rBend $rCore) (sphere)

    name vb12 (#neg $rPipe #neg $rBend 0)
    name vb13 (0 #neg $rBend #neg $rPipe)
    name vb14 ($rPipe #neg $rBend 0)
    name vb15 (0 #neg $rBend $rPipe)
);

blocks
(
    // X-pipe
    hex (vx0 vx1 vx2 vx3 vx8 vx9 vx10 vx11) pipes ($nCore $nCore $nLength) simpleGrading (1 1 1)
    hex (vx1 vx0 vx4 vx5 vx9 vx8 vx12 vx13) pipes ($nCore $nPipe $nLength) simpleGrading (1 1 1)
    hex (vx2 vx1 vx5 vx6 vx10 vx9 vx13 vx14) pipes ($nCore $nPipe $nLength) simpleGrading (1 1 1)
    hex (vx3 vx2 vx6 vx7 vx11 vx10 vx14 vx15) pipes ($nCore $nPipe $nLength) simpleGrading (1 1 1)
    hex (vx0 vx3 vx7 vx4 vx8 vx11 vx15 vx12) pipes ($nCore $nPipe $nLength) simpleGrading (1 1 1)

    // Y-lower-pipe
    hex (vl0 vl1 vl2 vl3 vl8 vl9 vl10 vl11) pipes ($nCore $nCore $nLength) simpleGrading (1 1 1)
    hex (vl1 vl0 vl4 vl5 vl9 vl8 vl12 vl13) pipes ($nCore $nPipe $nLength) simpleGrading (1 1 1)
    hex (vl2 vl1 vl5 vl6 vl10 vl9 vl13 vl14) pipes ($nCore $nPipe $nLength) simpleGrading (1 1 1)
    hex (vl3 vl2 vl6 vl7 vl11 vl10 vl14 vl15) pipes ($nCore $nPipe $nLength) simpleGrading (1 1 1)
    hex (vl0 vl3 vl7 vl4 vl8 vl11 vl15 vl12) pipes ($nCore $nPipe $nLength) simpleGrading (1 1 1)

    // Y-upper-pipe
    hex (vu0 vu1 vu2 vu3 vu8 vu9 vu10 vu11) pipes ($nCore $nCore $nLength) simpleGrading (1 1 1)
    hex (vu1 vu0 vu4 vu5 vu9 vu8 vu12 vu13) pipes ($nCore $nPipe $nLength) simpleGrading (1 1 1)
    hex (vu2 vu1 vu5 vu6 vu10 vu9 vu13 vu14) pipes ($nCore $nPipe $nLength) simpleGrading (1 1 1)
    hex (vu3 vu2 vu6 vu7 vu11 vu10 vu14 vu15) pipes ($nCore $nPipe $nLength) simpleGrading (1 1 1)
    hex (vu0 vu3 vu7 vu4 vu8 vu11 vu15 vu12) pipes ($nCore $nPipe $nLength) simpleGrading (1 1 1)

    // Ball
    hex (vb0 vb1 vb2 vb3 vb8 vb9 vb10 vb11)  ball ($nCore $nCore $nLength) simpleGrading (1 1 1)
    hex (vb1 vb0 vb4 vb5 vb9 vb8 vb12 vb13)  ball ($nCore $nPipe $nLength) simpleGrading (1 1 1)
    hex (vb2 vb1 vb5 vb6 vb10 vb9 vb13 vb14) ball ($nCore $nPipe $nLength) simpleGrading (1 1 1)
    hex (vb3 vb2 vb6 vb7 vb11 vb10 vb14 vb15) ball ($nCore $nPipe $nLength) simpleGrading (1 1 1)
    hex (vb0 vb3 vb7 vb4 vb8 vb11 vb15 vb12) ball ($nCore $nPipe $nLength) simpleGrading (1 1 1)
);

edges
(
    // X-pipe
    arc vx4 vx5 90 (1 0 0)
    arc vx5 vx6 90 (1 0 0)
    arc vx6 vx7 90 (1 0 0)
    arc vx7 vx4 90 (1 0 0)
    arc vx12 vx13 90 (1 0 0)
    arc vx13 vx14 90 (1 0 0)
    arc vx14 vx15 90 (1 0 0)
    arc vx15 vx12 90 (1 0 0)

    project vx8 vx9 (sphere)
    project vx9 vx10 (sphere)
    project vx10 vx11 (sphere)
    project vx11 vx8 (sphere)
    project vx8 vx12 (sphere)
    project vx9 vx13 (sphere)
    project vx10 vx14 (sphere)
    project vx11 vx15 (sphere)

    // Y-lower-pipe
    arc vl4 vl5 90 (0 1 0)
    arc vl5 vl6 90 (0 1 0)
    arc vl6 vl7 90 (0 1 0)
    arc vl7 vl4 90 (0 1 0)
    arc vl12 vl13 90 (0 1 0)
    arc vl13 vl14 90 (0 1 0)
    arc vl14 vl15 90 (0 1 0)
    arc vl15 vl12 90 (0 1 0)

    project vl8 vl9 (sphere)
    project vl9 vl10 (sphere)
    project vl10 vl11 (sphere)
    project vl11 vl8 (sphere)
    project vl8 vl12 (sphere)
    project vl9 vl13 (sphere)
    project vl10 vl14 (sphere)
    project vl11 vl15 (sphere)

    // Y-upper-pipe
    arc vu4 vu5 90 (0 -1 0)
    arc vu5 vu6 90 (0 -1 0)
    arc vu6 vu7 90 (0 -1 0)
    arc vu7 vu4 90 (0 -1 0)
    arc vu12 vu13 90 (0 -1 0)
    arc vu13 vu14 90 (0 -1 0)
    arc vu14 vu15 90 (0 -1 0)
    arc vu15 vu12 90 (0 -1 0)

    project vu8 vu9 (sphere)
    project vu9 vu10 (sphere)
    project vu10 vu11 (sphere)
    project vu11 vu8 (sphere)
    project vu8 vu12 (sphere)
    project vu9 vu13 (sphere)
    project vu10 vu14 (sphere)
    project vu11 vu15 (sphere)

    // Ball
    arc vb4 vb5 90 (1 0 0)
    arc vb5 vb6 90 (1 0 0)
    arc vb6 vb7 90 (1 0 0)
    arc vb7 vb4 90 (1 0 0)
    arc vb12 vb13 90 (0 -1 0)
    arc vb13 vb14 90 (0 -1 0)
    arc vb14 vb15 90 (0 -1 0)
    arc vb15 vb12 90 (0 -1 0)

    arc vb0 vb8 105 (0 0 -1)
    arc vb1 vb9 100 (0 0 -1)
    arc vb2 vb10 95 (0 0 -1)
    arc vb3 vb11 100 (0 0 -1)
    arc vb4 vb12 90 (0 0 -1)
    arc vb5 vb13 90 (0 0 -1)
    arc vb6 vb14 90 (0 0 -1)
    arc vb7 vb15 90 (0 0 -1)

    project vb0 vb1 (sphere)
    project vb1 vb2 (sphere)
    project vb2 vb3 (sphere)
    project vb3 vb0 (sphere)
    project vb0 vb4 (sphere)
    project vb1 vb5 (sphere)
    project vb2 vb6 (sphere)
    project vb3 vb7 (sphere)

    project vb8 vb9 (sphere)
    project vb9 vb10 (sphere)
    project vb10 vb11 (sphere)
    project vb11 vb8 (sphere)
    project vb8 vb12 (sphere)
    project vb9 vb13 (sphere)
    project vb10 vb14 (sphere)
    project vb11 vb15 (sphere)
);

faces
(
    // X-pipe
    project (vx8 vx9 vx10 vx11) sphere
    project (vx9 vx8 vx12 vx13) sphere
    project (vx10 vx9 vx13 vx14) sphere
    project (vx11 vx10 vx14 vx15) sphere
    project (vx8 vx11 vx15 vx12) sphere

    // Y-lower-pipe
    project (vl8 vl9 vl10 vl11) sphere
    project (vl9 vl8 vl12 vl13) sphere
    project (vl10 vl9 vl13 vl14) sphere
    project (vl11 vl10 vl14 vl15) sphere
    project (vl8 vl11 vl15 vl12) sphere

    // Y-upper-pipe
    project (vu8 vu9 vu10 vu11) sphere
    project (vu9 vu8 vu12 vu13) sphere
    project (vu10 vu9 vu13 vu14) sphere
    project (vu11 vu10 vu14 vu15) sphere
    project (vu8 vu11 vu15 vu12) sphere

    // Ball
    project (vb0 vb1 vb2 vb3) sphere
    project (vb1 vb0 vb4 vb5) sphere
    project (vb2 vb1 vb5 vb6) sphere
    project (vb3 vb2 vb6 vb7) sphere
    project (vb0 vb3 vb7 vb4) sphere

    project (vb8 vb9 vb10 vb11) sphere
    project (vb9 vb8 vb12 vb13) sphere
    project (vb10 vb9 vb13 vb14) sphere
    project (vb11 vb10 vb14 vb15) sphere
    project (vb8 vb11 vb15 vb12) sphere

    project (vb4 vb5 vb13 vb12) torus
    project (vb5 vb6 vb14 vb13) torus
    project (vb6 vb7 vb15 vb14) torus
    project (vb7 vb4 vb12 vb15) torus
);

defaultPatch
{
    name pipeWalls;
    type wall;
}

boundary
(
    inlet
    {
        type patch;
        faces
        (
            (vx0 vx1 vx2 vx3)
            (vx1 vx0 vx4 vx5)
            (vx2 vx1 vx5 vx6)
            (vx3 vx2 vx6 vx7)
            (vx0 vx3 vx7 vx4)
        );
    }
    lowerOutlet
    {
        type patch;
        faces
        (
            (vl0 vl1 vl2 vl3)
            (vl1 vl0 vl4 vl5)
            (vl2 vl1 vl5 vl6)
            (vl3 vl2 vl6 vl7)
            (vl0 vl3 vl7 vl4)
        );
    }
    upperOutlet
    {
        type patch;
        faces
        (
            (vu0 vu1 vu2 vu3)
            (vu1 vu0 vu4 vu5)
            (vu2 vu1 vu5 vu6)
            (vu3 vu2 vu6 vu7)
            (vu0 vu3 vu7 vu4)
        );
    }
    pipeNonCouple
    {
        type wall;
        faces
        (
            (vx8 vx9 vx10 vx11)
            (vx9 vx8 vx12 vx13)
            (vx10 vx9 vx13 vx14)
            (vx11 vx10 vx14 vx15)
            (vx8 vx11 vx15 vx12)

            (vl8 vl9 vl10 vl11)
            (vl9 vl8 vl12 vl13)
            (vl10 vl9 vl13 vl14)
            (vl11 vl10 vl14 vl15)
            (vl8 vl11 vl15 vl12)

            (vu8 vu9 vu10 vu11)
            (vu9 vu8 vu12 vu13)
            (vu10 vu9 vu13 vu14)
            (vu11 vu10 vu14 vu15)
            (vu8 vu11 vu15 vu12)
        );
    }
    ballWalls
    {
        type wall;
        faces
        (
            (vb4 vb5 vb13 vb12)
            (vb5 vb6 vb14 vb13)
            (vb6 vb7 vb15 vb14)
            (vb7 vb4 vb12 vb15)
        );
    }
    ballNonCouple
    {
        type wall;
        faces
        (
            (vb0 vb1 vb2 vb3)
            (vb1 vb0 vb4 vb5)
            (vb2 vb1 vb5 vb6)
            (vb3 vb2 vb6 vb7)
            (vb0 vb3 vb7 vb4)

            (vb8 vb9 vb10 vb11)
            (vb9 vb8 vb12 vb13)
            (vb10 vb9 vb13 vb14)
            (vb11 vb10 vb14 vb15)
            (vb8 vb11 vb15 vb12)
        );
    }
);

// ************************************************************************* //
